package com.baima46.builder;

import com.baima46.core.ExportHandle;
import com.baima46.core.InfoHandle;
import com.baima46.model.ExcelInfo;
import com.baima46.model.SheetInfo;

import java.io.File;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : yuan cong
 * @description: 多sheet页导出构建器
 * @date : 2021/5/17 16:36
 */
public class SheetBuilder {
    /**
     * excel导出信息
     */
    private ExcelInfo excelInfo;



    public SheetBuilder(String excelName) {
        excelInfo = new ExcelInfo();
        excelInfo.setFileName(excelName);
        excelInfo.setSheetList(new ArrayList<>());
    }
    /**
     * SheetBuilder，开始构建
     *
     * @return {@link SheetBuilder}
     */
    public static SheetBuilder create(String excelName) {
        return new SheetBuilder(excelName);
    }

    /**
     * 添加sheet页
     * @param sheetName sheet页名称
     * @param dataList 数据bean集合,支持bean使用注解设置表头
     * @param dataClass 数据bean的类对象
     * @param group 分组
     */
    public SheetBuilder addSheet(String sheetName, List<?> dataList, Class<?> dataClass, String... group) {
        SheetInfo sheetInfo = InfoHandle.getSheetInfo(sheetName, dataList, dataClass, group);

        excelInfo.getSheetList().add(sheetInfo);
        return this;
    }

    /**
     * 导出Excel，通过http response 导出，文件下载
     */
    public void export() {
        ExportHandle.exportResponse(excelInfo);
    }
    /**
     * 导出Excel，通过 输出流 导出，文件下载
     */
    public void export(OutputStream out) {
        ExportHandle.exportOut(excelInfo, out);
    }
    /**
     * 导出Excel，通过 文件 导出，文件下载
     */
    public void export(File file) {
        ExportHandle.exportFile(excelInfo, file);
    }
}
